<template>
  <Transition name="toast">
    <div v-if="visible" class="toast-container">
      <div class="toast" :class="currentType">
        <div class="toast-icon">
          <span v-if="currentType === 'success'" class="success-icon">✓</span>
          <span v-else-if="currentType === 'error'" class="error-icon">✗</span>
          <span v-else-if="currentType === 'warning'" class="warning-icon">⚠</span>
          <span v-else class="info-icon">ℹ</span>
        </div>
        <div class="toast-content">
          <div class="toast-message">{{ currentMessage }}</div>
        </div>
        <div class="toast-progress">
          <div class="progress-bar" :style="{ width: progressWidth + '%' }"></div>
        </div>
      </div>
    </div>
  </Transition>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';

const props = defineProps({
  message: {
    type: String,
    required: true
  },
  type: {
    type: String,
    default: 'success',
    validator: (value) => ['success', 'error', 'warning', 'info'].includes(value)
  },
  duration: {
    type: Number,
    default: 1000
  }
});

const visible = ref(false);
const progressWidth = ref(100);
const currentMessage = ref(props.message);
const currentType = ref(props.type);
const currentDuration = ref(props.duration);
let progressInterval = null;
let hideTimeout = null;

const show = (message = '', type = 'success', duration = 1000) => {
  // 如果传入了参数，更新props
  if (message) {
    // 这里我们需要动态更新props，但由于Vue 3的限制，我们使用一个不同的方法
    // 我们将使用一个响应式变量来存储当前显示的消息
    currentMessage.value = message;
    currentType.value = type;
    currentDuration.value = duration;
  }
  
  visible.value = true;
  progressWidth.value = 100;
  
  // 开始进度条动画
  const startTime = Date.now();
  const endTime = startTime + currentDuration.value;
  
  progressInterval = setInterval(() => {
    const currentTime = Date.now();
    const elapsed = currentTime - startTime;
    const remaining = Math.max(0, endTime - currentTime);
    progressWidth.value = (remaining / currentDuration.value) * 100;
    
    if (currentTime >= endTime) {
      hide();
    }
  }, 16); // 约60fps
  
  // 设置自动隐藏
  hideTimeout = setTimeout(() => {
    hide();
  }, currentDuration.value);
};

const hide = () => {
  visible.value = false;
  if (progressInterval) {
    clearInterval(progressInterval);
    progressInterval = null;
  }
  if (hideTimeout) {
    clearTimeout(hideTimeout);
    hideTimeout = null;
  }
};

// 暴露方法给父组件
defineExpose({
  show,
  hide
});

onUnmounted(() => {
  if (progressInterval) {
    clearInterval(progressInterval);
  }
  if (hideTimeout) {
    clearTimeout(hideTimeout);
  }
});
</script>

<style scoped>
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
  min-width: 300px;
  max-width: 400px;
  position: relative;
  overflow: hidden;
  pointer-events: auto;
}

.toast.success {
  border-left: 4px solid #10b981;
  background: linear-gradient(135deg, rgba(16, 185, 129, 0.1), rgba(16, 185, 129, 0.05));
}

.toast.error {
  border-left: 4px solid #ef4444;
  background: linear-gradient(135deg, rgba(239, 68, 68, 0.1), rgba(239, 68, 68, 0.05));
}

.toast.warning {
  border-left: 4px solid #f59e0b;
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.1), rgba(245, 158, 11, 0.05));
}

.toast.info {
  border-left: 4px solid #3b82f6;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(59, 130, 246, 0.05));
}

.toast-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-size: 14px;
  font-weight: bold;
  flex-shrink: 0;
}

.success-icon {
  color: #10b981;
  background: rgba(16, 185, 129, 0.1);
}

.error-icon {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

.warning-icon {
  color: #f59e0b;
  background: rgba(245, 158, 11, 0.1);
}

.info-icon {
  color: #3b82f6;
  background: rgba(59, 130, 246, 0.1);
}

.toast-content {
  flex: 1;
  min-width: 0;
}

.toast-message {
  font-size: 14px;
  font-weight: 500;
  color: #374151;
  line-height: 1.4;
  word-break: break-word;
}

.toast-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #10b981, #34d399);
  transition: width 0.1s ease;
}

.toast.success .progress-bar {
  background: linear-gradient(90deg, #10b981, #34d399);
}

.toast.error .progress-bar {
  background: linear-gradient(90deg, #ef4444, #f87171);
}

.toast.warning .progress-bar {
  background: linear-gradient(90deg, #f59e0b, #fbbf24);
}

.toast.info .progress-bar {
  background: linear-gradient(90deg, #3b82f6, #60a5fa);
}

/* 动画效果 */
.toast-enter-active,
.toast-leave-active {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.toast-enter-from {
  opacity: 0;
  transform: translateX(100%) scale(0.9);
}

.toast-leave-to {
  opacity: 0;
  transform: translateX(100%) scale(0.9);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .toast-container {
    top: 10px;
    right: 10px;
    left: 10px;
  }
  
  .toast {
    min-width: auto;
    max-width: none;
    width: 100%;
  }
}
</style> 